#ifndef	__SYSTEM_H__
#define __SYSTEM_H__

#include "CH543.H"    //CH543ͷ����
#include "DEBUG.H"    //ϵͳDebugͷ����


typedef enum {
    WORK_M_NULL=0,
    WORK_M_POWER_ON,
    WORK_M_PD,
    WORK_M_BC12_OR_QC,
    
    WORK_M_WAIT_QC_REQ_20V,
    WORK_REQ20V_WAIT,
    WORK_M_WAIT_QC_REQ_12V,
    WORK_REQ12V_WAIT,
    WORK_M_WAIT_QC_REQ_9V,
    WORK_REQ9V_WAIT,
    WORK_M_QC_REQ_SUCCCESS,
    WORK_M_UNKNOWN,
    WORK_M_MAX___
}WORK_MODE_T;

/** PD芯片内部 I2C reg map
 *
 * REG 0(RO): PD协议芯片内部固件版本信息, 格式: BCD code ,例如： V1.0==> 0x10, V1.3==>0x13;
 * REG 1(RO): 输入源信息
 *          b7~b5: Reserved
 *          b4: 1 => USB Input active; 0 => USB input inactive
 *          b3: 1 => DC Input active; 0 => DC input inactive
 *          b2~b0: USB input information
 *                  000: unknown
 *                  001: PD2.0
 *                  010: BC1.2
 *                  011: QC2.0
 *                  100: QC3.0
 *                  101: PD3.0
 *                  110: CDP
 *                  111: SDP
 *
 * REG 2(RW): 配置PD/QC交互信息
 *          b7~b4: Reserved
 *          b3~b0: 配置输入电压
 *              0000: 5V
 *              0001: 9V
 *              0010: 12V
 *              0011: 15V
 *              0100: 20V
 *
 *
 */
typedef enum {
    // *          b2~b0: USB input information
    // *                  000: unknown
    // *                  001: PD2.0
    // *                  010: BC1.2
    // *                  011: QC2.0
    // *                  100: QC3.0
    // *                  101: PD3.0
    // *                  110: CDP
    // *                  111: SDP
    USB_INFOR_unknown = 0,
    USB_INFOR_PD2_0,
    USB_INFOR_BC1_2,
    USB_INFOR_QC2_0,
    USB_INFOR_QC3_0,
    USB_INFOR_PD3_0,
    USB_INFOR_CDP,
    USB_INFOR_SDP,
} USB_INPUT_INFOR;

typedef enum {
    //  *          b3~b0: 配置输入电压
    //  *              0000: 5V
    //  *              0001: 9V
    //  *              0010: 12V
    //  *              0011: 15V
    //  *              0100: 20V
    USB_IN_CFG_5V = 0,
    USB_IN_CFG_9V,
    USB_IN_CFG_12V,
    USB_IN_CFG_15V,
    USB_IN_CFG_20V,

    USB_IN_CFG_MAX,
} USB_INPUT_CFG;

#define INPUT_CFG_DEFAULT USB_IN_CFG_20V
#define SW_VERSION 0x11
#define MAX_I2C_SLAVE_REG  3u
#define I2C_SLAVE_DEV_ADD 0x56
extern BOOL i2cs_flag;
extern BOOL i2cs_recv_flag;
extern UINT8X i2cs_reg_map[ MAX_I2C_SLAVE_REG ];

extern const UINT16 out_cfg_table[USB_IN_CFG_MAX];
extern const UINT16 out_cfg_table_for_QC[USB_IN_CFG_MAX];

#define SET_USB_IN_INFOR(a)            \
    {                                  \
        i2cs_reg_map[1] &= 0xf8;       \
        i2cs_reg_map[1] |= (0x07 & a); \
    }
#define SET_USB_IN_FLG(a)            \
    {                                \
        i2cs_reg_map[1] &= (~0x10);     \
        i2cs_reg_map[1] |= (a << 4); \
    }
#define SET_DC_IN_FLG(a)             \
    {                                \
        i2cs_reg_map[1] &= (~0X08);     \
        i2cs_reg_map[1] |= (a << 3); \
    }

void I2CS_Slave_Init(void);
UINT8 get_out_cfg(void);

#if 0
/*ϵͳѡ��ģʽ*/
#define System_Init_Mode      1
#define System_BC_Mode        2
#define System_QC2_Mode       3
#define System_QC3_Mode       4
#define System_Apple_Mode     5
#define System_PD_Mode        6
#define System_AFC_Mode       7
#define System_FCP_Mode       8


/*ADC�������*/
#define  bCC1_Aisle        0
#define  bCC2_Aisle        1
#define  Input_V_Aisle     2
#define  Input_I_Aisle     6
#define  bDP_Aisle         8
#define  bDM_Aisle         9

/*Read��ȡ״̬*/
#define  FlashReadData(dat)    dat =  *(PUINT8C)0x3800   //��ȡ��ǰ״̬

/*������������*/
sbit    Add_Button      = P1^7;  
sbit    Sub_Button      = P1^5;
sbit    Ack_Button      = P1^4;


/*X������*/
#define  OLED_X1_Pos      18

/*Y������*/
#define  OLED_Y0_Pos      1
#define  OLED_Y1_Pos      16
#define  OLED_Y2_Pos      32
#define  OLED_Y3_Pos      48
#define  OLED_Y4_Pos      64
#define  OLED_Y5_Pos      80
#define  OLED_Y6_Pos      96
#define  OLED_Y7_Pos      112
#define  OLED_Y8_Pos      128


/*�ⲿ��������*/
extern UINT8 System_Select_Mode;
extern UINT16 Key_Handle_Value;
extern UINT16 Key_List_Value;
extern UINT16 PPS_Send_Timer;
//extern UINT8 Key_Affirm_Value;
extern bit   Bit_Key_LongPree;
extern bit   Bit_Key_ShortPree;



/*��ʱ��*/
void Timer0Init(void);
void mTimer_x_SetData(UINT16 dat);

/*ADC*/
void ADC_Init(void);
UINT16 Check_ADC(UINT8 Aisle);

 /*ϵͳ��ʼ����������*/
void Init_Mian_Proc(void);

 /*ϵͳPD��������*/	
void PD_Mian_Proc(void);

 /*ϵͳBC��������*/	
void BC_Mian_Proc(void);

 /*ϵͳQC��������*/		
void QC_Mian_Proc(void);



UINT8 BC1_2Check_Proc(void);
void System_PPS_Display(void);
void System_A_Display(void);
 /*ϵͳ��ʼ��*/
void System_Init(void);

void System_PD_Display(UINT8 POSX,UINT8 POSY,UINT16 VoltValue,UINT8 Mult);


UINT8 FlashErasePage(UINT16 Addr);
UINT8 FlashProgByte(UINT16 Addr,UINT8 Data);
void System_Volt_Display(void);
void System_LHL_Display(float TempValue);
#endif

#endif
